package com.first.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.AclPermission;
import com.first.common.pojo.AclRolePermission;
import com.first.common.pojo.AclUser;
import com.first.common.vo.Result;
import com.first.system.service.PermissionService;
import com.first.system.vo.PermissionIdsVo;
import com.first.system.vo.PermissionInsertVo;
import com.first.system.vo.PermissionVo;
import com.first.system.vo.RolePermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: finrsteducation-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-05-23 20:42
 **/
@RestController
@RequestMapping("system/permission")
@Api(tags = "主页面接口类")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("update")
    @ApiOperation("菜单信息修改")
    public Result update(@RequestBody AclPermission permission) {
        System.out.println(permission);
        Integer update = permissionService.update(permission);
        if (update > 0) {
            return Result.success();
        }
        return Result.fail("信息修改失败");
    }

    @PutMapping("insert")
    @ApiOperation("主目录新增")
    public Result insert(@RequestBody PermissionInsertVo permissionInsertVo) {
        Integer insert = permissionService.insert(permissionInsertVo);
        return Result.success();
    }

    @GetMapping("getAllPermission")
    @ApiOperation("左侧导航列表类")
    public Result leftManu(HttpServletRequest request) {
        //获取请求头
        String token = request.getHeader("token");
        //从redis中取出对象
        String json = redisTemplate.opsForValue().get("user::" + token);
        AclUser aclUser = JSON.parseObject(json, AclUser.class);
        List<AclPermission> aclPermissionList = permissionService.leftMenu(aclUser.getId());
        //System.out.println(aclPermissionList);
        return Result.success(aclPermissionList);
    }

    @GetMapping("findPermissionByRoleId/{id}")
    @ApiOperation("角色权限修改数据回滚")
    public Result findPermissionByRole(@PathVariable String id) {
        System.out.println(id);
        List<AclPermission> permissionList = permissionService.findAll();
        List<AclRolePermission> rolePermissionList = permissionService.findById(id);
        List<String> idlist = rolePermissionList.stream().map(rolePermission ->
                rolePermission.getPermissionId()).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("allPermissions", permissionList);//所有的权限
        map.put("checkItems", idlist); //拥有的权限
        return Result.success(map);
    }

    @PostMapping("findAllByPage/{page}/{pageSize}")
    @ApiOperation("菜单管理条件分页查询")
    public Result findRolePAge(@PathVariable String page,
                               @PathVariable String pageSize,
                               @RequestBody PermissionVo permissionVo) {
//        System.out.println(page);
//        System.out.println(pageSize);
//        System.out.println(roleVo);
        Page<AclPermission> PermissionPage = permissionService.findPermissionPage(page, pageSize, permissionVo);
        return Result.success(PermissionPage);
    }

    @PostMapping("confirmFenPermission")
    @ApiOperation("角色权限修改")
    public Result updatePermissionByRole(@RequestBody RolePermissionVo rolePermissionVo) {
        Integer upda = permissionService.updateRolePersission(rolePermissionVo);
        if (upda > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    @PostMapping("deleteById")
    @ApiOperation("权限删除")
    public Result deleteByIds(@RequestBody PermissionIdsVo permissionIdsVo) {
       // System.out.println(permissionIdsVo.getPermissionIds());
        Integer deletede = permissionService.deleteByIds(permissionIdsVo.getPermissionIds());
        return Result.success();
    }


} 
